import Vue from 'vue'
import VueRouter from 'vue-router'
// import { Message } from 'element-ui'
// import { getToken } from '../cookie/index'

const Home = () =>
    import ('../views/home/Home.vue')
const Introduction = () =>
    import ('../views/introduction/Introduction.vue')
const Leave = () =>
    import ('../views/leave/Leave.vue')
const Login = () =>
    import ('../views/login/Login.vue')
const Register = () =>
    import ('../views/login/Register.vue')
const Blogs = () =>
    import ('../views/blogs/Blogs.vue')
const Particulars = () =>
    import ('../views/particulars/Particulars.vue')

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Home',
        component: Home,
        meta: {
            title: '首页'
        }
    },
    {
        path: '/introduction',
        name: 'Introduction',
        component: Introduction,
        meta: {
            title: '博主介绍'
        }
    },
    {
        path: '/leave',
        name: 'Leave',
        component: Leave,
        meta: {
            title: '留言板'
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: '登录'
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
        meta: {
            title: '注册'
        }
    },
    {
        path: '/blogs',
        name: 'Blogs',
        component: Blogs,
        meta: {
            title: '博客'
        }
    },
    {
        path: '/particular/:id',
        name: 'Particulars',
        component: Particulars
    }
]

const router = new VueRouter({
        mode: 'history',
        routes
    })
    //解决重复点击相同路径报错问题
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
    return VueRouterPush.call(this, to).catch(err => err)
}

// // 挂载路由导航守卫
// router.beforeEach((to, from, next) => {
//     // to 将要访问的路径， from 代表从哪个路径跳转而来， next 是一个函数，表示放行
//     if (to.path === '/login') {
//         return next()
//     }
//     if (to.path === '/register') {
//         return next()
//     }
//     const tokenStr = getToken()
//     if (!tokenStr) {
//         Message({
//             message: '请先登录账号，再进行其他操作',
//             type: 'info',
//             duration: 2 * 1000
//         })
//         return next('/login')
//     }
//     next()
// })

export default router